import { getSearch } from '@/services';
import { ISearchDatil } from '@/types';
import { Effect, Reducer } from 'umi';

export interface SearchModelState {
  search: Partial<ISearchDatil>[];
}

export interface SearchModelType {
  namespace: 'search';
  state: SearchModelState;
  effects: {
    getSearch: Effect;
  };
  reducers: {
    save: Reducer<SearchModelState>;
  };
}


const SettingModel: SearchModelType = {
  namespace: 'search',

  state: {
    search: []
  },

  effects: {
    *getSearch({ payload }, { call, put }) {
      if (payload != undefined) {
        let result = yield getSearch(payload);
        if (result.data) {
          yield put({
            type: 'save',
            payload: {
              search: result.data
            }
          })
        }
      }
    },
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },

  }
};


export default SettingModel;
